package com.tyqx.biz.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class IndexController {

    private static final Logger logger = LoggerFactory.getLogger(IndexController.class);

    @RequestMapping("/index1")
    public String index(@RequestParam(value = "pageIndex",required = false) Integer pageIndex, Model model){
        RestTemplate restTemplate = new RestTemplate();
        Map<String,String> params = new HashMap<>();
        params.put("address","0x62dD3a1d95DB1E4BeD53001291BF04c3f689B515");
        ResponseEntity<String> forEntity = restTemplate.getForEntity("https://api.f2pool.com/eth/{address}", String.class, params);
        String body = forEntity.getBody();
        /*Gson gson = new Gson();
        JsonObject f2poolReturnData = gson.fromJson(gson.toJson(body), JsonObject.class);*/
        JSONObject f2poolReturnData = JSON.parseObject(body);


        Object staticValue = "";
        if(f2poolReturnData.getString("value").equals("结算中")){
            staticValue = "结算中";
        }else{
            if(f2poolReturnData.getString("value").equals("0E-8")){
                staticValue = 0.00000000;
            }else{
                staticValue = f2poolReturnData.getBigDecimal("value").setScale(8,BigDecimal.ROUND_DOWN);
                //staticValue = staticValue.setScale(8,BigDecimal.ROUND_DOWN);
            }
        }

        Object paid = "";
        if(f2poolReturnData.getString("paid").equals("结算中")){
            paid = "结算中";
        }else{
            if(f2poolReturnData.getString("paid").equals("0E-8")){
                paid = 0.00000000;
            }else{
                paid = f2poolReturnData.getBigDecimal("paid").setScale(8,BigDecimal.ROUND_DOWN);
                //staticValue = staticValue.setScale(8,BigDecimal.ROUND_DOWN);
            }
        }

        Object todayPaid = "";
        if(f2poolReturnData.getString("today_paid").equals("结算中")){
            todayPaid = "结算中";
        }else{
            if(f2poolReturnData.getString("today_paid").equals("0E-8")){
                todayPaid = 0.00000000;
            }else{
                todayPaid = f2poolReturnData.getBigDecimal("today_paid").setScale(8,BigDecimal.ROUND_DOWN);
                //staticValue = staticValue.setScale(8,BigDecimal.ROUND_DOWN);
            }
        }

        Object valueToday = "";
        if(f2poolReturnData.getString("value_today").equals("结算中")){
            valueToday = "结算中";
        }else{
            if(f2poolReturnData.getString("value_today").equals("0E-8")){
                valueToday = 0.00000000;
            }else{
                valueToday = f2poolReturnData.getBigDecimal("value_today").setScale(8,BigDecimal.ROUND_DOWN);
                //staticValue = staticValue.setScale(8,BigDecimal.ROUND_DOWN);
            }
        }

        Object staticBalance = "";
        if(f2poolReturnData.getString("balance").equals("结算中")){
            staticBalance = "结算中";
        }else{
            if(f2poolReturnData.getString("balance").equals("0E-8")){
                staticBalance = 0.00000000;
            }else{
                staticBalance = f2poolReturnData.getBigDecimal("balance").setScale(8,BigDecimal.ROUND_DOWN);
                //staticValue = staticValue.setScale(8,BigDecimal.ROUND_DOWN);
            }
        }

        JSONArray payoutHistory = f2poolReturnData.getJSONArray("payout_history");
        JSONArray payoutHistoryFee = f2poolReturnData.getJSONArray("payout_history_fee");

        JSONArray payoutHistoryNew = new JSONArray();

        if(payoutHistory.size() > 0 && payoutHistoryFee.size() > 0){
            for (int i=0; i < payoutHistory.size(); i++){



                JSONArray payoutHistorySub = payoutHistory.getJSONArray(i);
                JSONArray payoutHistoryFeeSub = payoutHistoryFee.getJSONArray(i);


                Date payoutHistorySubDate = payoutHistorySub.getDate(0);
                String payoutHistorySubAddress = payoutHistorySub.getString(1);

                Date payoutHistoryFeeSubDate = payoutHistoryFeeSub.getDate(0);
                String payoutHistoryFeeSubAddress = payoutHistoryFeeSub.getString(1);

                if(payoutHistorySubDate.compareTo(payoutHistoryFeeSubDate) == 0){
                    JSONArray payoutHistoryNewSub = new JSONArray();
                    BigDecimal payoutHistorySubBigDecimal = payoutHistorySub.getBigDecimal(2);

                    BigDecimal payoutHistoryFeeSubDBigDecimal = payoutHistoryFeeSub.getBigDecimal(2);

                    BigDecimal payoutHistorySubBigDecimalCount = payoutHistorySubBigDecimal.add(payoutHistoryFeeSubDBigDecimal);
                    payoutHistorySubBigDecimalCount = payoutHistorySubBigDecimalCount.setScale(8,BigDecimal.ROUND_DOWN);

                    SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd" );
                    String payoutHistorySubDateFormat = "";
                    try {
                        payoutHistorySubDateFormat = sdf.format(payoutHistorySubDate);
                        payoutHistoryFeeSubDate = sdf.parse(sdf.format(payoutHistoryFeeSubDate));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    payoutHistoryNewSub.add(payoutHistorySubDateFormat);
                    payoutHistoryNewSub.add(payoutHistorySub.getString(1));
                    payoutHistoryNewSub.add(payoutHistorySubBigDecimalCount);
                    payoutHistoryNewSub.add(payoutHistorySub.getDouble(3));
                    payoutHistoryNew.add(payoutHistoryNewSub);
                }


            }
        }

        for (int j=0;j<payoutHistoryFee.size();j++){
            JSONArray payoutHistoryFeeJSONArray = payoutHistoryFee.getJSONArray(j);
            BigDecimal payoutHistoryFeeBigDecimal = payoutHistoryFeeJSONArray.getBigDecimal(2);
            payoutHistoryFeeBigDecimal = payoutHistoryFeeBigDecimal.setScale(8,BigDecimal.ROUND_DOWN);
            payoutHistoryFeeJSONArray.set(2,payoutHistoryFeeBigDecimal);

        }
        //payoutHistoryNew.sort();
        //payoutHistoryNew = ListSortDate.jsonArraySort(payoutHistoryNew);
        Collections.reverse(payoutHistoryNew);

        Integer pageSize = 10;
        Integer totalCount = payoutHistoryNew.size();
        Integer totalPage = (totalCount + pageSize - 1) / pageSize;
        Integer lastPage = 0;
        Integer nextPage = 0;
        Integer prePage = 0;

        List<Object> pageList = new ArrayList<>();
        //分页
        int fromIndex = 0;
        int toIndex = 0;
        if(pageIndex == null){
            pageIndex = 1;
            prePage = 1;
            fromIndex = (pageIndex - 1) * pageSize;
            if (fromIndex >= payoutHistoryNew.size()) {
                fromIndex = payoutHistoryNew.size() - 1;
            }
            toIndex = pageIndex * pageSize;
            if (toIndex >= payoutHistoryNew.size()) {
                toIndex = payoutHistoryNew.size();
            }
            pageList = payoutHistoryNew.subList(fromIndex, toIndex);
        }else{
            if(pageIndex>0){
                if(pageIndex>1) {
                    prePage = pageIndex - 1;
                }else{
                    prePage = 1;
                }
                fromIndex = (pageIndex - 1) * pageSize;
                if (fromIndex >= payoutHistoryNew.size()) {
                    fromIndex = payoutHistoryNew.size() - 1;
                }
                toIndex = pageIndex * pageSize;
                if (toIndex >= payoutHistoryNew.size()) {
                    toIndex = payoutHistoryNew.size();
                }
                pageList = payoutHistoryNew.subList(fromIndex, toIndex);
            }else{
                prePage = 1;
                pageIndex = 1;
                fromIndex = (pageIndex - 1) * pageSize;
                if (fromIndex >= payoutHistoryNew.size()) {
                    fromIndex = payoutHistoryNew.size() - 1;
                }
                toIndex = pageIndex * pageSize;
                if (toIndex >= payoutHistoryNew.size()) {
                    toIndex = payoutHistoryNew.size();
                }
                pageList = payoutHistoryNew.subList(fromIndex, toIndex);
            }
        }
        lastPage = pageIndex-1>1? pageIndex-1:1;
        nextPage = pageIndex>=totalPage? totalPage: pageIndex + 1;

        model.addAttribute("fromIndex",fromIndex+1);
        model.addAttribute("toIndex",toIndex);
        model.addAttribute("nextPage",nextPage);
        model.addAttribute("lastPage",lastPage);
        model.addAttribute("currentPage",pageIndex);
        model.addAttribute("totalCount",totalCount);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("prePage",prePage);

        model.addAttribute("f2poolReturnData",f2poolReturnData);
        model.addAttribute("address","0x62dD3a1d95DB1E4BeD53001291BF04c3f689B515");
        model.addAttribute("static_value",staticValue);
        model.addAttribute("paid",paid);
        model.addAttribute("today_paid",todayPaid);
        model.addAttribute("value_today",valueToday);
        model.addAttribute("static_balance",staticBalance);
        model.addAttribute("payoutHistoryNew",payoutHistoryNew);
        model.addAttribute("payoutHistoryFee",payoutHistoryFee);
        model.addAttribute("pageList",pageList);
        return "/index1";
    }

    public static void main(String[] args) {
        System.out.println( UUID.randomUUID().toString().replace("-", "").toUpperCase());
    }
}
